/************************公告管理js*********************************/
define(function(require, exports, module) {
	
	var $ = require("jquery1.7");
	var noticeBoardService = require("bean/noticeBoardService");
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var ui = require("ui");
	var table = layui.table;
	//添加公告
	$("#addNoticeBtn").bind("click",function(){
		location.href = ctx + "/back/website/noticeContent";
	});
	
	

	
	//批量删除
	$("#deleteNoticeBtn").bind("click", function(){
        var checkStatus = layui.table.checkStatus('test');
		if (checkStatus.data.length===0) {
			layer.alert("请选择公告");
			return;
		}
		layer.confirm("确定要删除公告？",{icon: 3, title:'提示'},function(isOk){
			if(isOk){
				var data = checkStatus.data;
				var array = [];
				for(var i = 0; i<data.length;i++){
					array.push(data[i].id);   
				}
				if (array.length != 0) {
					try{
						ui.mask().show("请稍候...");
						noticeBoardService.deleteNoticeList(array);
						ui.mask().hide();
						layer.alert("删除成功",function(){
							
							window.location.reload(true);
						});
					} catch (e) {
						ui.mask().hide();
						layer.alert("删除失败:"+e.message);
					}
				}
			}
		})
	});
	
	//发布公告
	$("#issueNoticeBtn").bind("click",function(){
		var checkStatus = layui.table.checkStatus('test');
		if (checkStatus.data.length===0) {
			layer.alert("请选择一条公告");
			return;
		}
		layer.confirm("确定要发布？",{icon: 3, title:'提示'},function(isOk){
			if(isOk){
				var data = checkStatus.data;
				var notice_id=data[0].id;
				try{
					ui.mask().show("请稍候...");
					noticeBoardService.issueNotice(notice_id);
					ui.mask().hide();
					layer.alert("发布成功",function(){
						
						window.location.reload(true);
					});
				}catch (e) {
					ui.mask().hide();
					layer.alert("发布失败:"+e.message);
				}
			}
		})
	});
	
	
	//修改公告
	$(document).on("click",".updateNotice",function(){
		var id = $(this).attr("data").replace("notice_", "");
		var notice=noticeBoardService.findNoticeById(id);
		if(notice.issueStatus!=0){
			layer.alert("已发布的公告不可修改");
			return;
		}
		location.href = ctx + "/back/website/noticeContent?noticeId="+id;
	});
	
	//公告置顶
	$(document).on("click",".noticeToTop",function(){
		var id = $(this).attr("data");
		noticeBoardService.noticeToTop(id);
		layer.alert("置顶成功！",function(){
			window.location.reload();
		});
	});
	
	//公告不置顶
	$(document).on("click",".noticeNoToTop",function(){
		var id = $(this).attr("data").replace("notice_", "");
		noticeBoardService.noticeNoToTop(id);
		layer.alert("成功取消置顶",function(){
			
			window.location.reload();
		});
	});
	//查看公告详细信息
	$(document).on("click",".checkNoticeDetail",function(){
		var id = $(this).attr("data").replace("notice_", "");
		window.open("/back/website/checkNoticeDetail/"+id, "_blank");
	});
	
	
	
	//layui的js代码

	var tablelin = table.render({
	    elem: '#demo'
	    ,url : '/back/website/noticeManagerData'
	    ,cols: [[ //标题栏
	      {checkbox:true, fixed: true}
	      ,{field: 'index', title: '序号', width: 80}
	      ,{field: 'title', title: '公告标题', width: 400}
	      ,{field: 'publisher', title: '发布人', width: 100}
	      ,{field: 'submiTime', title: '提交时间', width: 150}
	      ,{field: 'submiTime', title: '发布时间', width: 150}
	      ,{field: 'issueStatus', title: '发布状态', width: 150, templet:'#issueStatus'}
	      ,{field: '', title: '操作', fixed: 'right', width: 300,templet:'#barDemo'}
	    ]] 
	    ,even: true
	    ,page: true //是否显示分页
	    ,limits: [5, 10, 15,20,25,30]
	    ,limit: 5 //每页默认显示的数量
	    ,id: "test"
	  });
	//查询公告
	$(document).on("click","#notice_searchBtn",function(){		
		var param = {
		title:$("#notice_search_keyword").val()
		}
		tablelin.reload({
			where:param
		});
	})

});
